/**
 * Created by wangyin on 2017/11/16.
 */
define([
  'echarts',
  'pages/common/dashboard/dashboard.css'
], function (require, exports) {
  // page加载完成后调用ready方法
  exports.ready = function (hashCode, data, cite) {
    var newRoute = {
      route_20180107211030: {
        html: 'pages/example/package/demoAddMenuTab.html',
        js: 'pages/example/package/demoAddMenuTab.js'
      }
    };
    yufp.router.addRouteTable(newRoute);
    yufp.custom.vue({
      el: cite.el,
      data: function () {
        return {
          overviewDataUrl: 'use/homepage/Overview', // mock
          serviceDataUrl: 'api/use/monitor/homeMonitor/queryServiceStaticsList',
          taskDataUrl: 'api/use/monitor/homeMonitor/queryTaskStaticsList',
          workDataUrl: 'api/use/monitor/homeMonitor/queryJobStaticsList',
          resourceDataUrl: 'api/use/monitor/homeMonitor/querySourceStaticsList',
          eventDataUrl: 'api/use/monitor/homeMonitor/queryEventStaticsList',
          errorLineChartUrl: 'api/use/monitor/homeMonitor/queryErrorsLineCharts',
          warningLineChartUrl: 'api/use/monitor/homeMonitor/queryWarningsLineCharts',
          taskStaticsUrl: 'api/use/monitor/homeMonitor/queryTaskStatics',
          serviceStaticsUrl: 'api/use/monitor/homeMonitor/queryServiceStatics',
          eventStaticsUrl: 'api/use/monitor/homeMonitor/queryEventStatics',
          resourceStaticsUrl: 'api/use/monitor/homeMonitor/queryResourceStatics',
          /**
           *     private static final String ERROR_CNT_STR="errorCnt";
           private static final String ALL_CNT_STR="allCnt";
           */
          overview: {
            task: {
              allCnt: '',
              errorCnt: '',
            },
            service: {
              allCnt: '',
              errorCnt: '',
            },
            event: {
              allCnt: '',
              errorCnt: '',
            },
            resource: {
              allCnt: '',
              errorCnt: '',
            },
          },
          // 图表
          warnNum: '',
          errorNum: '',
          warnEchartOption: {},
          errorEchartOption: {},
          baseEchartOption: {
            tooltip: {
              trigger: 'axis',
              axisPointer: {
                type: 'shadow'
              },
              formatter: "日期:{b}<br>数量:{c}"
            },
            xAxis: {
              type: 'category',
              data: []/*,
              axisLabel: {
                show: true,
                interval: 'auto',
                formatter: ''
              },*/
            },
            yAxis: {
              type: 'value',
              show: false,
            },
            grid: {
              top: 10,
              right: 5,
              bottom: 10,
              left: 10,
              containLabel: true
            },
            series: [{
              data: [],
              type: 'line',
              showSymbol: false,
              smooth: true,
              color: ['#C7262B', '#0056CE'],
              itemStyle: {
                normal: {
                  lineStyle: {
                    width: 2,
                    color: {
                      type: 'linear',
                      x: 0.5,
                      y: 0,
                      x2: 0,
                      y2: 1,
                      colorStops: [{
                        offset: 0, color: '#C7262B'
                      }, {
                        offset: 1, color: '#C7262B'
                      }]
                    }
                  }
                }
              }
            }]
          },
          // 表格
          serviceTableData: [],
          taskTableData: [],
          workTableData: [],
          resourceTableData: [],
          eventTableData: [],
        }
      },
      created: function () {
        this.getOverviewData();
        this.getWarnEchartOption();
        this.getErrorEchartOption();
        this.getServiceTableData();
        this.getTaskAndWorkTableData();
        this.getResourceTableData();
        this.getEventTableData();
      },
      methods: {
        getOverviewData: function () {
          const _this = this;
          _this.getTaskStatics();
          _this.getServiceStatics();
          _this.getEventStatics();
          _this.getResourceStatics()
        },

        getTaskStatics: function () {
          const _this = this;
          yufp.service.request({
            method: 'get',
            url: backend.appOcaService + _this.taskStaticsUrl,
            callback: function (code, message, res) {
              if (code == 0) {
                _this.overview.task = res.data;
              }
            }
          });
        },

        getServiceStatics: function () {
          const _this = this;
          yufp.service.request({
            method: 'get',
            url: backend.appOcaService + _this.serviceStaticsUrl,
            callback: function (code, message, res) {
              if (code == 0) {
                _this.overview.service = res.data;
              }
            }
          });
        },

        getEventStatics: function () {
          const _this = this;
          yufp.service.request({
            method: 'get',
            url: backend.appOcaService + _this.eventStaticsUrl,
            callback: function (code, message, res) {
              if (code == 0) {
                console.log(res);
                _this.overview.event = res.data;
              }
            }
          });
        },
        getResourceStatics: function () {
          const _this = this;
          yufp.service.request({
            method: 'get',
            url: backend.appOcaService + _this.resourceStaticsUrl,
            callback: function (code, message, res) {
              if (code == 0) {
                console.log(res);
                _this.overview.resource = res.data;
              }
            }
          });
        },

        getWarnEchartOption: function () {
          var _this = this;
          var warnEchartOption = JSON.parse(JSON.stringify(this.baseEchartOption));
          yufp.service.request({
            method: 'get',
            url: backend.appOcaService + _this.warningLineChartUrl,
            callback: function (code, message, res) {
              if (code == 0) {
                _this.warnNum = res.data.currentCnt;
                warnEchartOption.xAxis.data = res.data.xAxisStrList;
                warnEchartOption.series[0].data = res.data.datas;
                warnEchartOption.series[0].itemStyle.normal.lineStyle.color.colorStops = [
                  { offset: 0, color: '#C7262B' },
                  { offset: 1, color: '#C7262B' }
                ]
                _this.warnEchartOption = warnEchartOption;
              }
            }
          });


        },
        getErrorEchartOption: function () {
          var _this = this;
          var errorEchartOption = JSON.parse(JSON.stringify(this.baseEchartOption));
          yufp.service.request({
            method: 'get',
            url: backend.appOcaService + _this.errorLineChartUrl,
            callback: function (code, message, res) {
              if (code == 0) {
                _this.errorNum = res.data.currentCnt;
                errorEchartOption.xAxis.data = res.data.xAxisStrList;
                errorEchartOption.series[0].data = res.data.datas;
                errorEchartOption.series[0].itemStyle.normal.lineStyle.color.colorStops = [
                  { offset: 0, color: '#0056CE' },
                  { offset: 1, color: '#0056CE' }
                ]
                _this.errorEchartOption = errorEchartOption;
              }
            }
          });
        },
        getServiceTableData: function () {
          var _this = this;
          yufp.service.request({
            method: 'get',
            url: backend.appOcaService + _this.serviceDataUrl,
            callback: function (code, message, res) {
              if (code == 0) {
                _this.serviceTableData = res.data;
              }
            }
          });
        },
        getTaskAndWorkTableData: function () {
          this.getTaskTableData();
          this.getWorkTableData();
        },
        getTaskTableData: function () {
          var _this = this;
          yufp.service.request({
            method: 'get',
            url: backend.appOcaService + _this.taskDataUrl,
            callback: function (code, message, res) {
              if (code == 0) {
                _this.taskTableData = res.data;
              }
            }
          });
        },
        getWorkTableData: function () {
          var _this = this;
          yufp.service.request({
            method: 'get',
            url: backend.appOcaService + _this.workDataUrl,
            callback: function (code, message, res) {
              if (code == 0) {
                _this.workTableData = res.data;
              }
            }
          });
        },
        getResourceTableData: function () {
          var _this = this;
          yufp.service.request({
            method: 'get',
            url: backend.appOcaService + _this.resourceDataUrl,
            callback: function (code, message, res) {
              if (code == 0) {
                _this.resourceTableData = res.data;
              }
            }
          });
        },
        getEventTableData: function () {
          var _this = this;
          yufp.service.request({
            method: 'get',
            url: backend.appOcaService + _this.eventDataUrl,
            callback: function (code, message, res) {
              if (code == 0) {
                _this.eventTableData = res.data;
              }
            }
          });
        }
      }
    })

    window.dashboardClickFn = function () {
      var customKey = 'custom_20180107211030'; // 请以custom_前缀开头，并且全局唯一
      var routeId = 'route_20180107211030'; // 模板示例->普通查询的路由ID
      yufp.frame.addTab({
        id: routeId, // 菜单功能ID（路由ID）
        key: customKey, // 自定义唯一页签key,请统一使用custom_前缀开头
        title: '报告详情', // 页签名称
        data: { custId: '1001001' } // 传递的业务数据，可选配置
      });
    };

    var setTabs = function () {
      $('.yu-tabs').each(function () {
        var _eN = $(this).attr('data-eventName') ? $(this).attr('data-eventName') : 'click';
        $(this).find('a').on(_eN, function () {
          $(this).addClass('selected').siblings().removeClass('selected');
          $(this).parent().parent().siblings('.yu-tabBox').eq($(this).index()).show().siblings('.yu-tabBox').hide();
        });
      });
    };

    // Demo相关js逻辑，仅示例，请开发自行实现完善
    setTabs();
  };

  // 消息处理
  exports.onmessage = function (type, message) {

  };

  // page销毁时触发destroy方法
  exports.destroy = function (id, cite) {

  };
});